Crate usvg

source · []
Expand description

usvg (micro SVG) is an SVG simplification tool.

Purpose

Imagine, that you have to extract some data from the SVG file, but your library/framework/language doesn’t have a good SVG library. And all you need is paths data.

You can try to export it by yourself (how hard can it be, right). All you need is an XML library (I’ll hope that your language has one). But soon you realize that paths data has a pretty complex format and a lot of edge-cases. And we didn’t mention attributes propagation, transforms, visibility flags, attribute values validation, XML quirks, etc. It will take a lot of time and code to implement this stuff correctly.

So, instead of creating a library that can be used from any language (impossible), usvg takes a different approach. It converts an input SVG to an extremely simple representation, which is still a valid SVG. And now, all you need is to convert your SVG to a simplified one via usvg and an XML library with some small amount of code.

Key features of the simplified SVG

  • No basic shapes (rect, circle, etc). Only paths
  • Simple paths:
    • Only MoveTo, LineTo, CurveTo and ClosePath will be produced
    • All path segments are in absolute coordinates
    • No implicit segment commands
    • All values are separated by space
  • All (supported) attributes are resolved. No implicit one
  • No use. Everything is resolved
  • No invisible elements
  • No invalid elements (like rect with negative/zero size)
  • No units (mm, em, etc.)
  • No comments
  • No DTD
  • No CSS (partial support)
  • No script (simply ignoring it)

Full spec can be found here.

Limitations

  • Currently, it’s not lossless. Some SVG features isn’t supported yet and will be ignored.
  • CSS support is minimal.
  • Scripting and animation isn’t supported and not planned.
  • a elements will be removed.
  • Unsupported elements:
    • some filter-based elements
    • font-based elements

Re-exports

pub use roxmltree;
pub use fontdb;

Modules

A collection of SVG filters.
Some useful utilities.

Structs

Representation of the preserveAspectRatio attribute.
A generic gradient.
A clip-path element.
A 8-bit RGB color.
An enable-background.
A fill style.
A group container.
A raster image element.
An xlink:href resolver for <image> elements.
A linear gradient.
A mask element.
A non-zero f64.
An immutable, finite f64 that is known to be > 0.
An immutable, finite f64 in a 0..=1 range.
Processing options.
A reference to processing options.
A path element.
A path bbox representation.
An SVG path data container.
A path segments iterator.
A pattern element.
A 2D point representation.
An immutable, finite f64 that is known to be >= 0.
A radial gradient.
A rect representation.
A 2D screen rect representation.
A 2D screen size representation.
A 2D size representation.
Gradient’s stop element.
A stroke style.
A stroke-miterlimit value.
Representation of the <transform> type.
An iterator over transformed path segments.
A nodes tree container.
View box.
XML writing options.

Enums

Representation of the align value of the preserveAspectRatio attribute.
List of all errors.
A fill rule.
Image fit options.
An embedded image kind.
An image rendering method.
A line cap.
A line join.
Node’s kind.
A paint style.
Representation of the paint-order property.
A path command.
A path’s absolute segment.
A shape rendering method.
A spread method.
A text rendering method.
An element units.
A visibility property.

Traits

A trait for approximate equality comparisons.
ApproxEqUlps is a trait for approximate equality comparisons. The associated type Flt is a floating point type which implements Ulps, and is required so that this trait can be implemented for compound types (e.g. vectors), not just for the floats themselves.
A trait for fuzzy/approximate equality comparisons of float numbers.
A trait for fuzzy/approximate comparisons of float numbers.
Checks that type has a default value.
Checks that the current number is > 0.
Additional Node methods.
Converts Rect into bbox Transform.

Type Definitions

A shorthand for ImageHrefResolver’s data function.
A shorthand for ImageHrefResolver’s string function.
Alias for rctree::Node<NodeKind>.
An alias to NormalizedF64.
An alias to NormalizedF64.
An alias to NonZeroPositiveF64.